/*
 * Copyright (c) 2012, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the CometMI Project.
 */
package org.cometmi.gwt.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.cometmi.gwt.shared.MiManager;
import org.cometmi.gwt.shared.MiProxyManager;
import org.cometmi.gwt.shared.MiQueue;
import org.cometmi.gwt.shared.MiTransaction;
import org.cometmi.gwt.shared.MiTransactionManager;
import org.cometmi.gwt.shared.events.AttributesCallback;
import org.cometmi.gwt.shared.events.BindCallback;
import org.cometmi.gwt.shared.exceptions.NotAllowedQueueException;
import org.cometmi.gwt.shared.request.Call;
import org.cometmi.gwt.shared.util.ProxyFactory;
import org.cometmi.gwt.shared.util.WrapperFactory;

/**
 * Implementation of a {@link MiQueue} class suitable for client side.
 * 
 * @author marco.tamburelli@gmail.com
 */
class MiQueueClient extends MiQueue
{
	private HashMap<String, String> attributes;
	protected boolean bindAllowed;

	protected boolean polling;

	MiQueueClient(String queueName, ProxyFactory proxyFactory, WrapperFactory wrapperFactory)
	{
		super();

		super.queueExpression = queueName;
		super.proxyFactory = proxyFactory;
		super.wrapperFactory = wrapperFactory;

		this.attributes = new HashMap<String, String>();
		this.bindAllowed = true;
		this.polling = false;
	}

	@Override
	protected MiManager provideMiManager(boolean transactional)
	{
		if (transactional)
			return new MiTransactionManager();
		else
			return new MiProxyManager()
			{
				@Override
				protected void publish(List<Call> calls)
				{
					Connection.INSTANCE.publish(super.queues.getExpression(), calls);
				}
			};
	}

	@Override
	public void bind(BindCallback callback)
	{
		if (callback == null) callback = new BindCallback()
		{
			@Override
			public void onSuccess()
			{}

			@Override
			public void onError(Throwable t)
			{}
		};

		/*
		 * If, for some reason, we already have this information, the error is
		 * caught immediately.
		 */
		if (!this.bindAllowed)
		{
			callback.onError(new NotAllowedQueueException("Queue " + this.queueExpression + " could be already bound"));
			return;
		}

		if (super.connected) return;

		super.connected = true;
		Connection.INSTANCE.listen(this, callback);
	}

	@Override
	public void unbind(BindCallback callback, boolean discard)
	{
		if (callback == null) callback = new BindCallback()
		{
			@Override
			public void onSuccess()
			{}

			@Override
			public void onError(Throwable t)
			{}
		};

		requestDisconnect();
		Connection.INSTANCE.dispose(this, callback, discard);
	}

	@Override
	protected void onResponse(List<Call> calls) throws Exception
	{
		manageResponse(calls);
	}

	@Override
	public MiTransaction createTransaction()
	{
		return new MiTransaction(this)
		{
			@Override
			protected void publish(List<Call> calls)
			{
				Connection.INSTANCE.publish(super.queues.getExpression(), calls);
			}
		};
	}

	@Override
	protected void forceDisconnect()
	{
		super.forceDisconnect();
	}

	@Override
	public void setAttribute(String key, String value, AttributesCallback callback)
	{
		if (callback == null) callback = new AttributesCallback()
		{
			@Override
			public void onError(Throwable t)
			{}

			@Override
			public void onSuccess(String value)
			{}
		};

		if (!this.bindAllowed)
			callback.onError(new NotAllowedQueueException("Queue " + this.queueExpression + " could be already bound"));

		this.attributes.put(key, value);

		if (this.polling) Connection.INSTANCE.setQueueAttribute(this.queueExpression, key, value, callback);
	}

	@Override
	public String getAttribute(String key, AttributesCallback callback)
	{
		String value = this.attributes.get(key);

		if (this.polling)
			Connection.INSTANCE.getQueueAttribute(this.queueExpression, key, callback);
		else
			callback.onSuccess(value);

		return value;
	}

	@Override
	public String removeAttribute(String key, AttributesCallback callback)
	{
		if (callback == null) callback = new AttributesCallback()
		{
			@Override
			public void onError(Throwable t)
			{}

			@Override
			public void onSuccess(String value)
			{}
		};

		if (!this.bindAllowed)
			callback.onError(new NotAllowedQueueException("Queue " + this.queueExpression + " could be already bound"));

		String value = this.attributes.remove(key);

		if (this.polling)
			Connection.INSTANCE.removeQueueAttribute(this.queueExpression, key, callback);
		else
			callback.onSuccess(value);

		return value;
	}

	Map<String, String> getAttributes()
	{
		return this.attributes;
	}
}
